package com.huaxun.edu.ucc.impl;

import java.util.List;

import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

import net.optix.common.app.ExecuteResult;
import net.optix.common.db.IBaseDAO;

import com.huaxun.edu.model.TBE_Circle;
import com.huaxun.edu.ucc.IJBUCC;

public class JBUCC implements IJBUCC {

	private IBaseDAO baseDAO;

	private TransactionTemplate transTemplate;

	@Override
	public List<TBE_Circle> findAllJBs(String xxdm) {
		return baseDAO.query("select * from TBE_Circle where xxdm='" + xxdm
				+ "'", null, TBE_Circle.class, 0, 0);
	}

	@Override
	public TBE_Circle findJB(String xxdm, String jb) {
		return baseDAO.queryForObject(
				"select * from TBE_Circle where xxdm='' and jb='" + jb + "'",
				null, TBE_Circle.class);
	}

	@Override
	public ExecuteResult saveJB(final String xxdm, final String jb,
			final TBE_Circle circle) {
		Object object = transTemplate.execute(new TransactionCallback() {

			ExecuteResult result = null;

			@Override
			public Object doInTransaction(TransactionStatus status) {

				try {
					long count = baseDAO
							.queryForLong(
									"select count(1) from tbe_circle where xxdm=? and jb=?",
									new Object[] { xxdm, jb });
					if (1 == count) {
						baseDAO.updateObject(circle, " and xxdm='" + xxdm
								+ "' and jb='" + jb + "'");
					} else {
						result = new ExecuteResult(-1, "届别已被删除或不存在");
					}
				} catch (Exception e) {
					status.setRollbackOnly();
					result = new ExecuteResult(-1, "更新届别:" + e.getMessage());
					e.printStackTrace();
				}
				return result;
			}
		});
		if (null != object && object instanceof ExecuteResult) {
			return (ExecuteResult) object;
		}
		return null;
	}

	@Override
	public ExecuteResult addJB(final String xxdm, final String jb,
			final TBE_Circle circle) {
		Object object = transTemplate.execute(new TransactionCallback() {

			ExecuteResult result = null;

			@Override
			public Object doInTransaction(TransactionStatus status) {

				try {
					long count = baseDAO
							.queryForLong(
									"select count(1) from tbe_circle where xxdm=? and jb=?",
									new Object[] { xxdm, jb });
					if (0 == count) {
						circle.setXxdm(xxdm);
						circle.setJb(jb);
						baseDAO.insertObject(circle);
						result = new ExecuteResult(1, "成功创建届别");
					} else {
						result = new ExecuteResult(-1, "届别代码被占用");
					}
				} catch (Exception e) {
					status.setRollbackOnly();
					result = new ExecuteResult(-1, "创建届别:" + e.getMessage());
					e.printStackTrace();
				}
				return result;
			}
		});
		if (null != object && object instanceof ExecuteResult) {
			return (ExecuteResult) object;
		}
		return null;
	}

	@Override
	public ExecuteResult deleteJB(String xxdm, String jb) {
		return null;
	}

	public IBaseDAO getBaseDAO() {
		return baseDAO;
	}

	public void setBaseDAO(IBaseDAO baseDAO) {
		this.baseDAO = baseDAO;
	}

	public TransactionTemplate getTransTemplate() {
		return transTemplate;
	}

	public void setTransTemplate(TransactionTemplate transTemplate) {
		this.transTemplate = transTemplate;
	}

}